import $ from 'jquery'
var oWebControl = null;// 插件对象
var playId = ''
var bIE = (!!window.ActiveXObject || 'ActiveXObject' in window);// 是否为IE浏览器
var pubKey = '';
var appkey = '25997254';
var secret = 'IWRyhPmK4KaPVWPSht8G';
// var secret = "pORWVVXnXsQq/s6V5C0SpxrPIYP4GZCz5WEEITBg3CHJeP9LTRVZ1C5j2NuZt7PQc8VemxlqT5yx1ixiEUZDV3wdAMam0u1BzEPNyxTlhKTF5AN1MCcp0Vz1FcgqvvysCwmQf2xuRtsYpF7kMKVRqm8FinxD/mA8YTEMZs8BUhQ="
var ip = '139.9.75.14';
var isHttps = '1';
var szPort='1443';
var snapDir = 'D:\\SnapDir';
var videoDir = 'D:\\VideoDir';
var layout = '3x3';
var encryptedFields = ['secret'];
var szShowToolbar = '1';
var szShowSmart = '1';
var btIds = '0,16,256,257,258,259,260,512,513,514,515,516,517,768,769';
var initCount = 0;

// 设置窗口控制回调
function setCallbacks() {
    oWebControl.JS_SetWindowControlCallback({
        cbIntegrationCallBack: cbIntegrationCallBack
    });
}
// 推送消息
function cbIntegrationCallBack(oData) {
    console.log(JSON.stringify(oData.responseMsg));
}
// 初始化插件
export function initPlugin (playId) {
    playId = playId
    return new Promise((resolve,reject) => {
        oWebControl = new WebControl({
            szPluginContainer: playId,
            iServicePortStart: 15900,
            iServicePortEnd: 15909,
            szClassId:"23BF3B0A-2C56-4D97-9C03-0CB103AA8F11",   // 用于IE10使用ActiveX的clsid
            cbConnectSuccess: function () {
                setCallbacks();			
                oWebControl.JS_StartService("window", {
                    dllPath: "./VideoPluginConnect.dll"
                }).then(function () {
                    
                    var width = $('#'+playId).width()
                    var heigth = $('#'+playId).height()
                    oWebControl.JS_CreateWnd(playId, width, heigth).then(function () {
                        console.log("JS_CreateWnd success");
                        resolve()						
                    });
                }, function () {
                
                });
            },
            cbConnectError: function () {
                console.log("cbConnectError");
                oWebControl = null;
                $("#"+playId).html("<div style='color:#fff;'>插件未启动，正在尝试启动，请稍候...</div>");
                WebControl.JS_WakeUp("VideoWebPlugin://");
                initCount ++;
                if (initCount < 3) {
                    setTimeout(function () {
                        initPlugin(playId);
                    }, 1000)
                } else {
                  
                    console.log("插件启动失败，请检查插件是否安装！")
                    $("#"+playId).html("<a href='https://yiyu-tools-1258963190.cos.ap-guangzhou.myqcloud.com/video/plugins/VideoWebPlugin.exe' download='VideoWebPlugin.exe' style='color:red;' href>您未安装插件，请点击下载插件，安装后，刷新页面即可</a>");
                }
            },
            cbConnectClose: function (bNormalClose) {
                // 异常断开：bNormalClose = false
                // JS_Disconnect正常断开：bNormalClose = true
                console.log("cbConnectClose");
                oWebControl = null;
            }
        });
    })
    
}

//initPlugin(); 

// 获取公钥
function getPubKey (callback) {
    oWebControl.JS_RequestInterface({
        funcName: "getRSAPubKey",
        argument: JSON.stringify({
            keyLength: 1024
        })
    }).then(function (oData) {
        console.log(oData)
        if (oData.responseMsg.data) {
            pubKey = oData.responseMsg.data
            callback()
        }
    })
}
 // RSA加密
 function setEncrypt (value) {
    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(pubKey);
    return encrypt.encrypt(value);
}
// 初始化
export function initVideo() {
    getPubKey(function () {
        secret = setEncrypt(secret);
        appkey = appkey.replace(/(^\s*)/g, "");
        appkey = appkey.replace(/(\s*$)/g, "");
        
        secret = secret.replace(/(^\s*)/g, "");
        secret = secret.replace(/(\s*$)/g, "");
        
        ip = ip.replace(/(^\s*)/g, "");
        ip = ip.replace(/(\s*$)/g, "");
        
        szPort = szPort.replace(/(^\s*)/g, "");
        szPort = szPort.replace(/(\s*$)/g, "");
        
        snapDir = snapDir.replace(/(^\s*)/g, "");
        snapDir = snapDir.replace(/(\s*$)/g, "");
        
        videoDir = videoDir.replace(/(^\s*)/g, "");
        videoDir = videoDir.replace(/(\s*$)/g, "");
        
        var port = parseInt(szPort);
        var showSmart = parseInt(szShowSmart);
        var showToolbar = parseInt(szShowToolbar);
        var enableHttps = parseInt(isHttps);
       
        encryptedFields = encryptedFields.join(",");

       
        // console.log({
        //     appkey: appkey,
        //     secret: secret,
        //     ip: ip,
        //     playMode: 0, // 预览
        //     port: port,
        //     snapDir: snapDir,
        //     videoDir: videoDir,
        //     layout: layout,
        //     enableHTTPS: enableHttps,
        //     showToolbar: showToolbar,
        //     showSmart: showSmart,
        //     buttonIDs: btIds,
        //     encryptedFields: encryptedFields
        // })

        oWebControl.JS_RequestInterface({
            funcName: "init",
            argument: JSON.stringify({
                appkey: appkey,
                secret: secret,
                ip: ip,
                playMode: 0, // 预览
                port: port,
                snapDir: snapDir,
                videoDir: videoDir,
                layout: layout,
                enableHTTPS: enableHttps,
                showToolbar: showToolbar,
                showSmart: showSmart,
                buttonIDs: btIds,
                encryptedFields: encryptedFields
            })
        }).then(function (oData) {
            console.log(JSON.stringify(oData ? oData.responseMsg : ''));
            // UpdatePlayTypeValue();
            // UpdateSnapTypeValue();
            // UpdateSetOSDTypeValue();
            var width = $('#'+playId).width()
            var heigth = $('#'+playId).height()
            oWebControl.JS_Resize(width, heigth);  // 初始化后resize一次，规避firefox下首次显示窗口后插件窗口未与DIV窗口重合问题
        });
    })
};

export function uninit () {
    oWebControl.JS_RequestInterface({
        funcName: "uninit"
    }).then(function (oData) {
        console.log(oData)
    });
}

 // 视频预览
 export function startPreview(cameraIndexCode) {
    
    var streamMode = 0;
    var transMode = 1;
    var gpuMode = 0;
    var wndId = -1;  //默认为空闲窗口回放
    
    cameraIndexCode = cameraIndexCode.replace(/(^\s*)/g, "");
    cameraIndexCode = cameraIndexCode.replace(/(\s*$)/g, "");
    
   
   

    oWebControl.JS_RequestInterface({
        funcName: "startPreview",
        argument: JSON.stringify({
            cameraIndexCode : cameraIndexCode ,
            streamMode: streamMode,
            transMode: transMode,
            gpuMode: gpuMode,
            wndId: wndId
        })
    }).then(function (oData) {
        console.log(JSON.stringify(oData ? oData.responseMsg : ''));
    });
}